<template>
  <!-- 常规登录 -->
  <div class="common-login">
    <el-form-item label="用户名" prop="username">
      <el-input
        type="text"
        v-model="loginForm.username"
        autocomplete="off"
      ></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        type="password"
        v-model="loginForm.password"
        autocomplete="off"
      ></el-input>
    </el-form-item>
    <el-form-item label="验证码" prop="captcha">
      <el-input
        maxlength="5"
        class="captcha"
        type="text"
        v-model="loginForm.captcha"
        @keydown.native.enter="submitForm('loginForm')"
      ></el-input>
      <span class="captcha-svg" v-loading="loading">
        <i v-html="captcha" @click="getCaptcha"></i>
      </span>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('loginForm')">
        提交
      </el-button>
    </el-form-item>
    <el-form-item>
      <div class="sms">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-iphone"></use>
        </svg>
        <a
          href="javascript:;"
          @click="$emit('handleSmsType')"
          style="color: #fff; font-size: 16px"
        >
          切换短信登入
        </a>
      </div>
    </el-form-item>
  </div>
</template>

<script>
import * as api from "@/api/users";
export default {
  data() {
    return {
      isSmsLogin: false, //控制是否切换短信登入
      captcha: "", //验证码
      loading: false, //验证码加载动画
      loginForm: {
        username: "",
        password: "",
        captcha: "",
      },
    };
  },
  created() {
    this.getCaptcha();
  },
  watch: {
    loginForm: {
      handler(newVal) {
        // console.log(newVal);
        this.$emit("syncLoginForm", newVal);
      },
      deep: true,
    },
  },
  methods: {
    submitForm(formName) {
      // console.log(formName);
      this.$emit("submit");
    },
    //封装一个获取验证码的方法
    getCaptcha() {
      //打开验证码加载动画
      this.loading = true;
      api.getCaptcha().then((res) => {
        this.captcha = res.data.img;
        //关闭验证码加载动画
        this.loading = false;
      });
    },
  },
};
</script>
<style lang="less" scoped>
.common-login {
  padding-right: 50px;
  .el-form-item {
    margin-bottom: 22px;
    /deep/.el-form-item__label {
      color: #fff !important;
    }
    /deep/.el-form-item__content {
      line-height: 40px !important;
      position: relative;
      font-size: 14px;
      .el-input {
        position: relative;
        font-size: 14px;
        display: inline-block;
        width: 100%;
        .el-input__inner {
          width: 250px;
        }
      }
      .captcha {
        width: 100px;
        vertical-align: top;
        .el-input__inner {
          width: 100%;
          height: 45px;
          border-radius: 5px 0 0 5px;
          border: 0;
          font-size: 18px;
        }
      }
      .captcha-svg {
        background: #fff;
        display: inline-block;
        height: 45px;
        width: 150px;
        text-align: center;
        cursor: pointer;
        border: 0;
        border-radius: 0 5px 5px 0;
      }
      .el-button {
        background: linear-gradient(90deg, #1596fb, #002dff);
        width: 250px;
      }
      .sms {
        margin-left: -15px !important;
        width: 250px !important;
        .icon {
          vertical-align: middle;
          fill: currentColor;
          overflow: hidden;
          width: 2em;
          height: 2em;
          margin-right: 5px;
        }
      }
    }
  }
}
</style>
